package day12;

/**
 * @author: 王丹
 * @create: 2021-07-03 14:20
 * 使用3个线程来对一个共享变量++操作
 **/
public class Demo2 {

    private static int num = 0;

    public static synchronized void add() {
        num++;
    }

    public static class T extends Thread {
        @Override
        public void run() {
            for (int i = 0; i < 1000; i++) {
                add();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        T t1 = new T();
        T t2 = new T();
        T t3 = new T();

        t1.start();
        t2.start();
        t3.start();

        t1.join();
//        t2.join();
//        t3.join();

        System.out.println(num);
    }
}
